package com.elem.web;

import com.elem.service.BusinessService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.elem.bean.Business;
import com.elem.bean.User;
import com.elem.service.BusinessService;
import com.elem.service.UserService;
import com.elem.service.impl.BusinessServiceImpl;
import com.elem.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

// 目前只有两个：一个是登录，一个是注册
@WebServlet("/BusinessController/*")
public class BusinessServlet extends BaseServlet {

    BusinessService businessService = new BusinessServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    public void getBusinessById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String businessId = req.getParameter("businessId");
            Business business = businessService.getBusinessById(businessId);
            objectMapper.writeValue(resp.getWriter(), business);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
    public void listBusinessByOrderTypeId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取提交的参数：分类id
        String orderTypeId = req.getParameter("orderTypeId");
        try {
            List<Business> businessList =
                    businessService.listBusinessByOrderTypeId(orderTypeId);
            objectMapper.writeValue(resp.getOutputStream(), businessList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}